import { HttpException, Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import { MenuEntity } from '../menu/entities/menu.entity'
import { FavoriteService } from '../favorite/favorite.service'
import { MenuDetailDto } from './dto/menu-detail.dto'

@Injectable()
export class MenuDetailService {
  constructor(
    @InjectRepository(MenuEntity)
    private readonly menuRepository: Repository<MenuEntity>,
    private readonly favoriteService: FavoriteService, // 收藏服务
  ) { }

  // 获取菜品详情
  async getDetail(userId: number, data: MenuDetailDto) {
    const menu = await this.menuRepository.findOne({
      where: { id: data.id, is_del: 0 },
      relations: ['detail'],
    })

    if (!menu) throw new HttpException('菜品不存在', 404)
    // if (!menu.detail) throw new HttpException('菜品详情不存在', 404)

    // 检查收藏状态
    const isFavorite = await this.favoriteService.checkFavorite(userId, data.id)

    return {
      id: menu.id,
      name: menu.name,
      fee: menu.fee,
      photo_url: menu.photo_url,
      type: menu.type || 0,
      description: menu.detail?.description || '',
      ingredients: menu.detail?.ingredients || '',
      // create_time: menu.create_time,
      // update_time: menu.update_time,
      sales: menu.sales || 0,
      is_new: menu.is_new || 0,
      is_favorite: isFavorite || false,
    }
  }
}